'use strict';

var debug = require('debug'),
  log = debug('yozh:mw:log'),
  error = debug('yozh:mw:error'),
  verbose = debug('yozh:mw:verbose');

/**
 * responseCache middleware
 * @param  {Object} opts options
 * @return {Function} ES6 generator function
 */
module.exports = function(opts) {
  var middleware = function *responseCache(next) {
    var method = this.request.method;
    if (method === 'GET' || method === 'get' || method === 'Get') {
      // this middleware after the requestCache middleware, so this is a new
      // query, so just save it to the redis
      var cur = new Date().toUTCString();
      var eres = yield opts.cache.set(
        method + this.path + this.querystring, cur
        );
      this.set({
        'Last-Modified': cur,
        'Cache-Control': 'max-age=' + opts.cache.defaultCacheTime
      });
      /*var exists = yield opts.cache.exists(
        method + this.path + this.querystring
      );
      if (exists === 1) {
        // this query has a cache, set Last-Modified is last updated time;
        var res = yield opts.cache.get(method + this.path + this.querystring);
        this.set({
          'Last-Modified': res,
          'Cache-Control': 'max-age=' + opts.cache.defaultCacheTime
        });
      } else {
        // this is a new query
        var cur = new Date().toUTCString();
        var eres = yield opts.cache.set(
          method + this.path + this.querystring, cur
          );
        this.set({
          'Last-Modified': cur,
          'Cache-Control': 'max-age=' + opts.cache.defaultCacheTime
        });
      }*/
      // this.log.info('Get Response: status: %s, Last-Modified: %s',
      //   this.status, this.get('Last-Modified'));
      verbose('Get Response: status: ', this.status,
        'Last-Modified: %s', this.get('Last-Modified'));
    } else {
      if (this.status >= 200 || this.status < 300) {
        // success status;
        // write method, modify date;
        // var cur = new Date().toUTCString();
        var wcur = this.state.updatedAt;
        if (wcur) {
          // var wres = yield opts.cache.set(
          //   method + this.path + this.querystring, wcur
          // );
          // add this object last updated time to redis;
          var wres = yield opts.cache.set(this.state.id, wcur);
          this.set({
            'Last-Modified': wcur,
            'Cache-Control': 'max-age=' + opts.cache.defaultCacheTime
          });
          // find all of url cache in redis, and remove or update
          // eg: post a new user and status is success, so it need to remove
          // the redis cache with user, like GET/v1/user/id;

          // this.log.info('Set key: %s%s%s, redis res: %s, curTime: %s',
          //   method, this.path, this.querystring, wres, wcur);
          verbose('Set key: ', method, this.path, this.querystring,
            'redis res: ', wres, 'curTime: ', wcur);
        }
      } else {
        // error status;
        // this.log.info('%s %s%s can not save entity',
        //   method, this.path, this.querystring);
        verbose(method, this.path, this.querystring, ' can not save entity');
      }
    }
    yield next;
  };
  return middleware;
};
